package com.yndsoft.jc.ui.memberpage.setting;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.Feedback;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.dialog.SupplierDialog;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/5.
 */

public class SuggestionsActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;
    @Bind(R.id.base_home)
    RelativeLayout base_home;

    @Bind(R.id.feed_back_name)
    EditText feed_back_name;
    @Bind(R.id.feed_back_cell)
    EditText feed_back_cell;
    @Bind(R.id.feed_back_content)
    EditText feed_back_content;
    @Bind(R.id.suggestion_postBtn)
    Button suggestion_postBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suggestions);

        setViews();
        setListener();
    }

    private void setViews() {
        base_title.setText(getResources().getString(R.string.feed_back));
        base_home.setVisibility(View.VISIBLE);
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
        suggestion_postBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
            case R.id.suggestion_postBtn:
                if (TextUtils.isEmpty(feed_back_name.getText())) {
                    showHint("姓名不能为空");
                } else {
                    if (TextUtils.isEmpty(feed_back_cell.getText())) {
                        showHint("联系方式不能为空");
                    } else {
                        if (TextUtils.isEmpty(feed_back_content.getText())) {
                            showHint("内容不能为空");
                        } else {
                            postSuggetions();
                        }
                    }
                }
                break;
        }
    }

    private void postSuggetions() {
        Feedback feedback = new Feedback();
        feedback.setName(feed_back_name.getText() + "");
        feedback.setPhone(feed_back_cell.getText() + "");
        feedback.setNote(feed_back_content.getText() + "");
        mHttpUtil.postSuggestions(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "提交失败");
                        break;
                    case 1:
                        ToastUtil.show(mContext, "提交成功");
                        finish();
                        break;
                }
            }
        }, "feedback", new Gson().toJson(feedback));
    }

    private void showHint(String content) {
        new SupplierDialog(mContext).show(false, "提示", content);
    }
}
